package edu.gatech.volt2.drdoommgt.presenter;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import edu.gatech.volt2.drdoommgt.domain.*;
import edu.gatech.volt2.drdoommgt.gui.IUserInfoView;
import edu.gatech.volt2.drdoommgt.system.DataBaseAccessor;

public class EditUserInfoPresenter implements ActionListener{
	private static DataBaseAccessor dba;
	private IUserInfoView myView;
	private Patient p;
	private Doctor d;
	private Nurse n;
	private SysAdmin sa;
	private int type;
	
	public EditUserInfoPresenter(int type, IUserInfoView view){
		this.dba= new DataBaseAccessor();
		this.dba.connectToDatabase();

		this.myView= view;
		this.myView.addListener(this);
		this.type= type;
		System.out.println(this.type);
		if(type==0)
		{
			this.p = this.editPatientProcess();
		}
		else if(type==1)
		{
			this.d = this.editDoctorProcess();
		}
		else if(type==2)
		{
			this.n = this.editNurseProcess();
		}
		else if(type==3)
		{
			this.sa = this.editSysAdminProcess();
		}
		else{
			//do nothing. the sys admin canceled the task
		}
		
		//this.myView.showSelf();
	}
	
	public EditUserInfoPresenter(Patient p, IUserInfoView view){
		this.dba= new DataBaseAccessor();
		this.dba.connectToDatabase();

		this.myView= view;
		this.myView.addListener(this);
		this.type= 0;


		this.p = this.editPatientProcess(p);
	}

	public void actionPerformed(ActionEvent action) {
		if(action.getSource()== myView.getCancelButton()){
			myView.hideSelf();
		}
		else if(action.getSource()== myView.getSaveButton()){
			
			if(type==0)
			{
				if(this.p != null){
					
					this.updatePatient();
					System.out.println(p.getName().toString());
					dba.saveUser(p);
				}
			}
			else if(type==1)
			{
				if(this.d != null){
					
					this.updateDoctor();
					dba.saveUser(d);
				}
			}
			else if(type==2)
			{
				if(this.n != null){
					
					this.updateNurse();
					dba.saveUser(n);
				}
			}
			else if(type==3)
			{
				if(this.sa != null){
					
					this.updateSysAdmin();
					dba.saveUser(sa);
				}
			}
			else{
				//do nothing. the sys admin canceled the task
			}
			
			myView.hideSelf();
		}
		else{/*do nothing*/}
		
	}
	
	private Patient editPatientProcess(){		
		this.p= null;
		
		ArrayList<Patient> patientList= dba.getAllPatients();
		Patient[] patientArr= new Patient[patientList.size()];
		for(int i = 0; i<patientArr.length; i++){
			patientArr[i]= patientList.get(i);
		}
		
		String[] pNamesandEmails= new String[patientArr.length];
		for(int i = 0; i<patientArr.length; i++){
			pNamesandEmails[i]= patientArr[i].getName().toString() + " - " + patientArr[i].getEmail();
		}
		
		String chosen= (String)JOptionPane.showInputDialog(null, "Choose a patient.", "Edit Patient", JOptionPane.QUESTION_MESSAGE, null, pNamesandEmails, pNamesandEmails[0]);
		if(chosen != null){
			int index= this.getChosenIndex(pNamesandEmails, chosen);
		
			this.p= patientList.get(index);
			
			JTextField firstNameField= myView.getFirstNameField();
			JTextField middleNameField= myView.getMiddleNameField();
			JTextField lastNameField= myView.getLastNameField();
			JTextField zipCodeField= myView.getZipCodeField();
			JTextField emailField= myView.getEmailField();
			JTextField phone= myView.getPhone();
			JTextField addressField= myView.getAddressField();
			JTextField cityField= myView.getCityField();
			JRadioButton rdbtnMale= myView.getRdbtnMale();
			JRadioButton rdbtnFemale= myView.getRdbtnFemale();
			JComboBox monthBox= myView.getMonthBox();
			JComboBox dayBox= myView.getDayBox();
			JComboBox yearBox= myView.getYearBox();
			JComboBox stateComboBox= myView.getStateComboBox();
			
			firstNameField.setText(this.p.getName().getFirst());
			
			middleNameField.setText(this.p.getName().getMiddle());
			lastNameField.setText(this.p.getName().getLast());
			emailField.setText(this.p.getEmail());

			phone.setText(this.p.getInfo().getPhone());
			addressField.setText(this.p.getInfo().getAddress().getStreetAddress());
			cityField.setText(this.p.getInfo().getAddress().getCity());
			stateComboBox.setSelectedItem(this.p.getInfo().getAddress().getState());
			zipCodeField.setText(this.p.getInfo().getAddress().getZipCode());
			
			Calendar bday= this.p.getInfo().getBirthday();

			int day= bday.get(Calendar.DAY_OF_MONTH);
			int month= bday.get(Calendar.MONTH) + 1; //the "+1" is due to the format of the Calendar object
			int year= bday.get(Calendar.YEAR);

			if(month<10)
				monthBox.setSelectedItem("0"+Integer.toString(month));
			else
				monthBox.setSelectedItem(Integer.toString(month));
			
			if(day<10)
				dayBox.setSelectedItem("0"+Integer.toString(day));
			else
				dayBox.setSelectedItem(Integer.toString(day));
			
			yearBox.setSelectedItem(Integer.toString(year));
			
			if(this.p.getInfo().getGender()== "male"){
				rdbtnMale.setSelected(true);
			}
			else{
				rdbtnFemale.setSelected(true);
			}				
			
			this.myView.showSelf();
					
		}
		return this.p;

	}
	
	private Patient editPatientProcess(Patient pat){		
		this.p= null;
		
		ArrayList<Patient> patientList= dba.getAllPatients();
		Patient[] patientArr= new Patient[patientList.size()];
		for(int i = 0; i<patientArr.length; i++){
			patientArr[i]= patientList.get(i);
		}
		
		String[] pNamesandEmails= new String[patientArr.length];
		for(int i = 0; i<patientArr.length; i++){
			pNamesandEmails[i]= patientArr[i].getName().toString() + " - " + patientArr[i].getEmail();
		}
		
		String chosen= pat.getName().toString() + " - " + pat.getEmail();
		if(chosen != null){
			int index= this.getChosenIndex(pNamesandEmails, chosen);
		
			this.p= patientList.get(index);
			
			JTextField firstNameField= myView.getFirstNameField();
			JTextField middleNameField= myView.getMiddleNameField();
			JTextField lastNameField= myView.getLastNameField();
			JTextField zipCodeField= myView.getZipCodeField();
			JTextField emailField= myView.getEmailField();
			JTextField phone= myView.getPhone();
			JTextField addressField= myView.getAddressField();
			JTextField cityField= myView.getCityField();
			JRadioButton rdbtnMale= myView.getRdbtnMale();
			JRadioButton rdbtnFemale= myView.getRdbtnFemale();
			JComboBox monthBox= myView.getMonthBox();
			JComboBox dayBox= myView.getDayBox();
			JComboBox yearBox= myView.getYearBox();
			JComboBox stateComboBox= myView.getStateComboBox();
			
			firstNameField.setText(this.p.getName().getFirst());
			
			middleNameField.setText(this.p.getName().getMiddle());
			lastNameField.setText(this.p.getName().getLast());
			emailField.setText(this.p.getEmail());

			phone.setText(this.p.getInfo().getPhone());
			addressField.setText(this.p.getInfo().getAddress().getStreetAddress());
			cityField.setText(this.p.getInfo().getAddress().getCity());
			stateComboBox.setSelectedItem(this.p.getInfo().getAddress().getState());
			zipCodeField.setText(this.p.getInfo().getAddress().getZipCode());
			
			Calendar bday= this.p.getInfo().getBirthday();

			int day= bday.get(Calendar.DAY_OF_MONTH);
			int month= bday.get(Calendar.MONTH) + 1; //the "+1" is due to the format of the Calendar object
			int year= bday.get(Calendar.YEAR);

			if(month<10)
				monthBox.setSelectedItem("0"+Integer.toString(month));
			else
				monthBox.setSelectedItem(Integer.toString(month));
			
			if(day<10)
				dayBox.setSelectedItem("0"+Integer.toString(day));
			else
				dayBox.setSelectedItem(Integer.toString(day));
			
			yearBox.setSelectedItem(Integer.toString(year));
			
			if(this.p.getInfo().getGender()== "male"){
				rdbtnMale.setSelected(true);
			}
			else{
				rdbtnFemale.setSelected(true);
			}				
			
			this.myView.showSelf();
					
		}
		return this.p;
	}
	
	private Doctor editDoctorProcess(){		
		this.d= null;
		
		ArrayList<Doctor> doctorList= dba.getAllDoctors();
		Doctor[] doctorArr= new Doctor[doctorList.size()];
		for(int i = 0; i<doctorArr.length; i++){
			doctorArr[i]= doctorList.get(i);
		}
		
		String[] dNamesandEmails= new String[doctorArr.length];
		for(int i = 0; i<doctorArr.length; i++){
			dNamesandEmails[i]= doctorArr[i].getName().toString() + " - " + doctorArr[i].getEmail();
		}
		
		String chosen= (String)JOptionPane.showInputDialog(null, "Choose a doctor.", "Edit Doctor", JOptionPane.QUESTION_MESSAGE, null, dNamesandEmails, dNamesandEmails[0]);
		if(chosen != null){
			int index= this.getChosenIndex(dNamesandEmails, chosen);
			
			this.d= doctorList.get(index);
			
			JTextField firstNameField= myView.getFirstNameField();
			JTextField middleNameField= myView.getMiddleNameField();
			JTextField lastNameField= myView.getLastNameField();
			JTextField emailField= myView.getEmailField();
		
			firstNameField.setText(this.d.getName().getFirst());
			middleNameField.setText(this.d.getName().getMiddle());
			lastNameField.setText(this.d.getName().getLast());
			emailField.setText(this.d.getEmail());

			
			
			this.myView.showSelf();
					
		}
		return this.d;

	}
	
	private Nurse editNurseProcess(){
//		dba= new DataBaseAccessor();
//		dba.connectToDatabase();
		
		this.n= null;
		
		ArrayList<Nurse> nurseList= dba.getAllNurses();
		Nurse[] nurseArr= new Nurse[nurseList.size()];
		for(int i = 0; i<nurseArr.length; i++){
			nurseArr[i]= nurseList.get(i);
		}
		
		String[] nNamesandEmails= new String[nurseArr.length];
		for(int i = 0; i<nurseArr.length; i++){
			nNamesandEmails[i]= nurseArr[i].getName().toString() + " - " + nurseArr[i].getEmail();
		}
		
		String chosen= (String)JOptionPane.showInputDialog(null, "Choose a nurse.", "Edit Nurse", JOptionPane.QUESTION_MESSAGE, null, nNamesandEmails, nNamesandEmails[0]);
		if(chosen != null){
			int index= this.getChosenIndex(nNamesandEmails, chosen);
			
			this.n= nurseList.get(index);
			
			JTextField firstNameField= myView.getFirstNameField();
			JTextField middleNameField= myView.getMiddleNameField();
			JTextField lastNameField= myView.getLastNameField();
			JTextField emailField= myView.getEmailField();
		
			firstNameField.setText(this.n.getName().getFirst());
			middleNameField.setText(this.n.getName().getMiddle());
			lastNameField.setText(this.n.getName().getLast());
			emailField.setText(this.n.getEmail());

			
			
			this.myView.showSelf();
					
		}
		return this.n;

	}
	
	private SysAdmin editSysAdminProcess(){
//		dba= new DataBaseAccessor();
//		dba.connectToDatabase();
		
		this.sa= null;
		
		ArrayList<SysAdmin> sysAdminList= dba.getAllSysAdmins();
		SysAdmin[] sysAdminArr= new SysAdmin[sysAdminList.size()];
		for(int i = 0; i<sysAdminArr.length; i++){
			sysAdminArr[i]= sysAdminList.get(i);
		}
		
		String[] saNamesandEmails= new String[sysAdminArr.length];
		for(int i = 0; i<sysAdminArr.length; i++){
			saNamesandEmails[i]= sysAdminArr[i].getName().toString() + " - " + sysAdminArr[i].getEmail();
		}
		
		String chosen= (String)JOptionPane.showInputDialog(null, "Choose a System Administrator.", "Edit System Administrator", JOptionPane.QUESTION_MESSAGE, null, saNamesandEmails, saNamesandEmails[0]);
		if(chosen != null){
			int index= this.getChosenIndex(saNamesandEmails, chosen);
			
			this.sa= sysAdminList.get(index);
			
			JTextField firstNameField= myView.getFirstNameField();
			JTextField middleNameField= myView.getMiddleNameField();
			JTextField lastNameField= myView.getLastNameField();
			JTextField emailField= myView.getEmailField();
		
			firstNameField.setText(this.sa.getName().getFirst());
			middleNameField.setText(this.sa.getName().getMiddle());
			lastNameField.setText(this.sa.getName().getLast());
			emailField.setText(this.sa.getEmail());

			
			
			this.myView.showSelf();
					
		}
		return this.sa;

	}

	public Name getName(){
		
		String name= JOptionPane.showInputDialog("Enter the full name.");
		String[] nameArray= name.split(" ");
		String first= null;
		String middle= null;
		String last= null;
		
		if(nameArray.length < 1){
			JOptionPane.showMessageDialog(null, "No name entered");
		}
		else if(nameArray.length < 2){
			first= nameArray[0];
		}
		else if(nameArray.length < 3){
			first= nameArray[0];
			last= nameArray[1];
		}
		else{
			first= nameArray[0];
			middle= nameArray[1];
			last= nameArray[2];
		}
		
		Name n= new Name(first, middle, last);
		return n;
	}

	private void updatePatient(){
		
		JTextField firstNameField= myView.getFirstNameField();
		JTextField middleNameField= myView.getMiddleNameField();
		JTextField lastNameField= myView.getLastNameField();
		JTextField zipCodeField= myView.getZipCodeField();
		JTextField emailField= myView.getEmailField();
		JTextField phone= myView.getPhone();
		JTextField addressField= myView.getAddressField();
		JTextField cityField= myView.getCityField();
		JRadioButton rdbtnMale= myView.getRdbtnMale();
		JRadioButton rdbtnFemale= myView.getRdbtnFemale();
		JComboBox monthBox= myView.getMonthBox();
		JComboBox dayBox= myView.getDayBox();
		JComboBox yearBox= myView.getYearBox();
		JComboBox stateComboBox= myView.getStateComboBox();
		
		
		Name name= new Name(firstNameField.getText(),middleNameField.getText(),lastNameField.getText());
		Address addr= new Address(addressField.getText(), cityField.getText(), (String)stateComboBox.getSelectedItem(), zipCodeField.getText());
		Calendar birthday= Calendar.getInstance();
		birthday.set(Integer.parseInt((String)yearBox.getSelectedItem()), Integer.parseInt((String)monthBox.getSelectedItem()) -1, Integer.parseInt((String)dayBox.getSelectedItem()));
		String gender= (rdbtnMale.isSelected() ? "male" : "female");
				
		this.p.setName(name);
		this.p.setEmail(emailField.getText());
		this.p.getInfo().setGender(gender);
		this.p.getInfo().setBirthday(birthday);
		this.p.getInfo().setPhone(phone.getText());
		this.p.getInfo().setAddress(addr);
				
	}
	
	private void updateDoctor(){
		JTextField firstNameField= myView.getFirstNameField();
		JTextField middleNameField= myView.getMiddleNameField();
		JTextField lastNameField= myView.getLastNameField();

		JTextField emailField= myView.getEmailField();
		
		
		Name name= new Name(firstNameField.getText(),middleNameField.getText(),lastNameField.getText());
	
		this.d.setName(name);
		this.d.setEmail(emailField.getText());
	}
	
	private void updateNurse(){
		JTextField firstNameField= myView.getFirstNameField();
		JTextField middleNameField= myView.getMiddleNameField();
		JTextField lastNameField= myView.getLastNameField();

		JTextField emailField= myView.getEmailField();
		
		
		Name name= new Name(firstNameField.getText(),middleNameField.getText(),lastNameField.getText());
	
		this.n.setName(name);
		this.n.setEmail(emailField.getText());
	
	}
	
	private void updateSysAdmin(){
		JTextField firstNameField= myView.getFirstNameField();
		JTextField middleNameField= myView.getMiddleNameField();
		JTextField lastNameField= myView.getLastNameField();

		JTextField emailField= myView.getEmailField();
		
		
		Name name= new Name(firstNameField.getText(),middleNameField.getText(),lastNameField.getText());
	
		this.sa.setName(name);
		this.sa.setEmail(emailField.getText());

	}
	
	
	private int getChosenIndex(String[] arr, String target){
		int i=0;
		boolean found= false;
		
		while(!found){
			if(arr[i].equals(target)){
				found= true;
			}
			else{
				i++;
			}
		}
		return i;
	}
	
}
